﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data;


namespace Autorun
{
	public partial class BrowseFileDialog : Form
	{
		private ServerManagement _proxy;

		#region "Constructors"
		public BrowseFileDialog()
		{
			InitializeComponent();
		}
		#endregion

		#region "Properties"
		public string AssemblyPath
		{
			get
			{
				if ((tvFolder.SelectedNode != null) && (lstFile.SelectedItems.Count != 0))
					return Path.Combine(tvFolder.SelectedNode.Name, lstFile.SelectedItems[0].SubItems[1].Text);
				else
					return null;
			}
		}

		public string AssemblyName
		{
			get
			{
				if (lstFile.SelectedItems.Count != 0)
					return lstFile.SelectedItems[0].SubItems[1].Text;
				else
					return null;
			}
		}

		public string ClassName
		{
			get
			{
				if (lstFile.SelectedItems.Count != 0)
					return lstFile.SelectedItems[0].Text;
				else
					return null;
			}
		}
		#endregion

		public void SetData(ServerManagement proxy)
		{
			DriveInfo[] di;
			DataTable dt;

			Cursor = Cursors.WaitCursor;

			_proxy = proxy;

			di = _proxy.FileAccess.GetDrives();

			dt = new DataTable();
			dt.Columns.Add("ValueMember");
			dt.Columns.Add("DisplayMember");

			foreach (DriveInfo item in di)
			{
				if (item.IsReady == true)
					dt.Rows.Add(
						item.Name,
						string.Format("{0} - {1}", item.Name, (item.VolumeLabel == null || item.VolumeLabel.Length == 0 ? string.Format("({0})", item.DriveType) : item.VolumeLabel)));
			}

			cbDrive.DisplayMember = "DisplayMember";
			cbDrive.ValueMember = "ValueMember";
			cbDrive.DataSource = dt;

			Cursor = Cursors.Default;
		}


		#region "Event Handlers"
		private void cbDrive_SelectedIndexChanged(object sender, EventArgs e)
		{
			string[] folders;
			string parentFolder;
			TreeNode node;

			Cursor = Cursors.WaitCursor;

			tvFolder.Nodes.Clear();
			if (cbDrive.SelectedIndex != -1)
			{
				parentFolder = cbDrive.SelectedValue.ToString();
				folders = _proxy.FileAccess.GetDirectories(parentFolder);
				if (folders != null)
					foreach (string item in folders)
					{
						node = tvFolder.Nodes.Add(item, item.Substring(parentFolder.Length));
						node.Nodes.Add("$" + item, "<Load SubFolders>");
					}
			}

			Cursor = Cursors.Default;
		}

		private void tvFolder_AfterCollapse(object sender, TreeViewEventArgs e)
		{
			e.Node.Nodes.Clear();
			e.Node.Nodes.Add("$" + e.Node.Name, "<Load SubFolders>");

		}

		private void tvFolder_BeforeExpand(object sender, TreeViewCancelEventArgs e)
		{
			string[] folders;
			string parentFolder;
			TreeNode node;

			parentFolder = e.Node.Name;
			e.Node.Nodes.Clear();
			folders = _proxy.FileAccess.GetDirectories(parentFolder);
			if (folders != null)
				foreach (string item in folders)
				{
					node = e.Node.Nodes.Add(item, item.Substring(parentFolder.Length + 1));
					node.Nodes.Add("$" + item, "<Load SubFolders>");
				}
		}

		private void tvFolder_AfterSelect(object sender, TreeViewEventArgs e)
		{
			SortedList<string, string> files;
			ListViewItem lvi;

			lstFile.Items.Clear();
			files = _proxy.FileAccess.GetFiles(e.Node.Name);
			for (int i = 0; i < files.Count; i++)
			{
				lvi = lstFile.Items.Add(files.Keys[i]);
				lvi.SubItems.Add(files.Values[i]);
			}

			lstFile.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
			lstFile.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);

		}

		private void btOK_Click(object sender, EventArgs e)
		{
			if (lstFile.SelectedItems.Count != 0)
			{
				DialogResult = DialogResult.OK;
			}
		}
		#endregion

		private void tvFolder_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				tvFolder.SelectedNode.Expand();
				e.SuppressKeyPress = true;
			}
		}
	}
}
